home *** CD-ROM | disk | FTP | other *** search
- Path: cnn.Princeton.EDU!franck!tim
- From: tim@franck.Princeton.EDU (Tim Hollebeek)
- Newsgroups: comp.lang.c++
- Subject: Re: size of vtable
- Date: 10 Jan 1996 19:17:15 GMT
- Organization: Princeton University
- Message-ID: <4d13br$9jf@cnn.Princeton.EDU>
- References: <4cr1ob$1cu@seal.abalon.se> <30f2dee6.0@news.sbbs.se>
- NNTP-Posting-Host: franck.princeton.edu
-
- In article <30f2dee6.0@news.sbbs.se>, Urban Fosseus <ufo@sbbs.se> wrote:
- >In article <4cr1ob$1cu@seal.abalon.se>, ken1@abalon.se says...
- >>
- >>Is it possible to know the size of the vtbl for a given class
- >>at compile time (to do something like
- >>
- >>int x = sizeof(theclass::vtbl)
- >>
- >>or similar)?
-
- First, the obvious: Why do you care? Ignoring that ...
-
- >If you have control over the base class, you can
- >evaluate the offset of the first data member with code like
- >
- >int sz_vtab = ((int)(&((myclass *)1)->data)-1);
-
- Many compilers put the vtable before the pointer, for efficiency; your
- hack would return zero on such implementations. Even without that
- problem, your code returns the size of the vtable _pointer_ not the
- table, which is a far less useful; probably even a constant
- (sizeof(void *)). Did you actually test this before posting?
-
- >Both approaches might fail if the compiler aligns data members and
- >rounds struct/class sizes in unpredicted ways. Strictly speaking,
- >the vtable itself is not required by the standard, but its effects.
- >So this is not strictly portable code, but code that you can prove
- >works for all interesting platforms ;-)
-
- On a SGI using g++, your 'working' code evaluates to zero, independent
- of vtable size. Since SGIs are interesting platforms, and g++ is an
- extremely common compiler, I conclude you haven't a clue what you are
- talking about.
-
- Not all interesting platforms use vtables, by a long shot. As shown
- above, your code will not do anything useful on _any_ platform, which
- leads me to believe you wrote your article off the top of your head.
-
- Please don't post useless garbage.
-
- --
- Tim Hollebeek | Everything above is a true statement, for sufficiently
- PChem Grad Student | false values of true.
- Princeton Univ. | tim@wfn-shop.princeton.edu
- -------------------| http://wfn-shop.princeton.edu/~tim
-